//
//  LJJBuyCarTableView.m
//  shopTest
//
//  Created by 梁佳佳 on 2016/11/22.
//  Copyright © 2016年 梁佳佳. All rights reserved.
//

#import "LJJBuyCarTableView.h"
#import "LJJBuyCarModel.h"
#import "LJJBuyCarTableViewCell.h"

@interface LJJBuyCarTableView () <UITableViewDelegate, UITableViewDataSource>



@end

@implementation LJJBuyCarTableView

static NSString *identifier = @"LJJBuyCarCell";

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        self.bounces = NO;
        [self registerClass:[LJJBuyCarTableViewCell class] forCellReuseIdentifier:identifier];
    }
    return self;
}
#pragma mark -
#pragma mark - method
- (void)setBuyCarModelArray:(NSMutableArray *)buyCarModelArray
{
    _buyCarModelArray = buyCarModelArray;
    for (LJJBuyCarModel *model in buyCarModelArray) {
        [model setIsSelectButton:YES];
    }
    if (buyCarModelArray.count > 4) {
        self.bounces = YES;
    }
    else
    {
        self.bounces = NO;
    }
    [self reloadData];
}
- (void)addButtonMethod:(UIButton *)sender
{
    UIView *cell = [sender superview];
    NSInteger index = cell.tag - 10000;
    LJJBuyCarModel *model = self.buyCarModelArray[index];
    NSInteger goodsCount = model.GoodsCount;
    goodsCount = goodsCount + 1;
    [model setGoodsCount:goodsCount];
    [self changeDataMethod];
}
- (void)subButtonMethod:(UIButton *)sender
{
    UIView *cell = [sender superview];
    NSInteger index = cell.tag - 10000;
    LJJBuyCarModel *model = self.buyCarModelArray[index];
    NSInteger goodsCount = model.GoodsCount;
    if (goodsCount > 1) {
        goodsCount = goodsCount - 1;
        [model setGoodsCount:goodsCount];
    }
    [self changeDataMethod];
}
- (void)isSelectedMethod:(UIButton *)sender
{
    UIView *cell = [sender superview];
    NSInteger index = cell.tag - 10000;
    LJJBuyCarModel *model = self.buyCarModelArray[index];
    sender.selected = !sender.selected;
    [model setIsSelectButton:sender.selected];
    [self changeDataMethod];
}
#pragma mark -
#pragma mark - tableView delegate & datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.buyCarModelArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJJBuyCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    LJJBuyCarModel *model = self.buyCarModelArray[indexPath.row];
    cell.tag = 10000 + indexPath.row;
    cell.buyCarModel = model;
    [cell.addButton addTarget:self action:@selector(addButtonMethod:) forControlEvents:UIControlEventTouchUpInside];
    [cell.subButton addTarget:self action:@selector(subButtonMethod:) forControlEvents:UIControlEventTouchUpInside];
    [cell.isSelectButton addTarget:self action:@selector(isSelectedMethod:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *action =[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        LJJBuyCarModel *cellModel = self.buyCarModelArray[indexPath.row];
        [cellModel setGoodsCount:0];
        [self changeDataMethod];
        [self.buyCarModelArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
    return @[action];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJJBuyCarModel *model = self.buyCarModelArray[indexPath.row];
    NSString *goodsID = model.GoodsId;
    if (_detailBlock) {
        _detailBlock(goodsID);
    }
}
- (void)changeDataMethod
{
    [self reloadData];
    if (_changeDataBlock) {
        _changeDataBlock();
    }
}

@end
